#include "bsp_PT100.h"
#include "bsp_ADC.h"
#include "bsp_Timer.h"
#include "stm32g030xx.h"
#include "User_App.h"

#define REFERENCE_MV 3300 // mV 参考电压
#define MEASURE_R 100     // Ω 测量电阻

PT100_t PT100 = {
    .Measure_mV = 0,
    .Measure_R = 0,
};

#define ROWS 42                   // 行数
#define COLS 10                   // 列数
const int Temp_Of_PT100[ROWS] = { // 相当于坐标系的Y坐标轴
    -10, 0,
    0, 10, 20, 30, 40, 50, 60, 70, 80, 90,
    100, 110, 120, 130, 140, 150, 160, 170, 180, 190,
    200, 210, 220, 230, 240, 250, 260, 270, 280, 290,
    300, 310, 320, 330, 340, 350, 360, 370, 380, 390};
// 阻值表，对应温度从 -10 到 390°C，每行 10 个值 (0~9)
const float Table_Of_PT100[ROWS][COLS] = {                                                    // 相当于坐标系的X坐标轴
    /*00*/ {96.09, 95.69, 95.3, 94.91, 94.52, 94.12, 93.73, 93.34, 92.95, 92.55},             //-10,
    /*01*/ {100.00, 99.61, 99.22, 98.83, 98.44, 98.04, 97.65, 97.26, 96.87, 96.48},           // 0,
    /*02*/ {100.00, 100.39, 100.78, 101.17, 101.56, 101.95, 102.34, 102.73, 103.12, 103.51},  // 0,
    /*03*/ {103.90, 104.29, 104.68, 105.07, 105.46, 105.85, 106.24, 106.63, 107.02, 107.40},  // 10,
    /*04*/ {107.79, 108.18, 108.57, 108.96, 109.35, 109.73, 110.12, 110.51, 110.90, 111.29},  // 20,
    /*05*/ {111.67, 112.06, 112.45, 112.83, 113.22, 113.61, 114.00, 114.38, 114.77, 115.15},  // 30,
    /*06*/ {115.54, 115.93, 116.31, 116.70, 117.08, 117.47, 117.86, 118.24, 118.63, 119.01},  // 40,
    /*07*/ {119.40, 119.78, 120.17, 120.55, 120.94, 121.32, 121.71, 122.09, 122.47, 122.86},  // 50,
    /*08*/ {123.24, 123.63, 124.01, 124.39, 124.78, 125.16, 125.54, 125.93, 126.31, 126.69},  // 60,
    /*09*/ {127.08, 127.46, 127.84, 128.22, 128.61, 128.99, 129.37, 129.75, 130.13, 130.52},  // 70,
    /*10*/ {130.90, 131.28, 131.66, 132.04, 132.42, 132.80, 133.18, 133.57, 133.95, 134.33},  // 80,
    /*11*/ {134.71, 135.09, 135.47, 135.85, 136.23, 136.61, 136.99, 137.37, 137.75, 138.13},  // 90,
    /*12*/ {138.51, 138.88, 139.26, 139.64, 140.02, 140.40, 140.78, 141.16, 141.54, 141.91},  // 100,
    /*13*/ {142.29, 142.67, 143.05, 143.43, 143.80, 144.18, 144.56, 144.94, 145.31, 145.69},  // 110,
    /*14*/ {146.07, 146.44, 146.82, 147.20, 147.57, 147.95, 148.33, 148.70, 149.08, 149.46},  // 120,
    /*15*/ {149.83, 150.21, 150.58, 150.96, 151.33, 151.71, 152.08, 152.46, 152.83, 153.21},  // 130,
    /*16*/ {153.58, 153.96, 154.33, 154.71, 155.08, 155.46, 155.83, 156.20, 156.58, 156.95},  // 140,
    /*17*/ {157.33, 157.70, 158.07, 158.45, 158.82, 159.19, 159.56, 159.94, 160.31, 160.68},  // 150,
    /*18*/ {161.05, 161.43, 161.80, 162.17, 162.54, 162.91, 163.29, 163.66, 164.03, 164.40},  // 160,
    /*19*/ {164.77, 165.14, 165.51, 165.89, 166.26, 166.63, 167.00, 167.37, 167.74, 168.11},  // 170,
    /*20*/ {168.48, 168.85, 169.22, 169.59, 169.96, 170.33, 170.70, 171.07, 171.43, 171.80},  // 180,//
    /*21*/ {172.17, 172.54, 172.91, 173.28, 173.65, 174.02, 174.38, 174.75, 175.12, 175.49},  // 190,
    /*22*/ {175.86, 176.22, 176.59, 176.96, 177.33, 177.69, 178.06, 178.43, 178.79, 179.16},  // 200,
    /*23*/ {179.53, 179.89, 180.26, 180.63, 180.99, 181.36, 181.72, 182.09, 182.46, 182.82},  // 210,
    /*24*/ {183.19, 183.55, 183.92, 184.28, 184.65, 185.01, 185.38, 185.74, 186.11, 186.47},  // 220,
    /*25*/ {186.84, 187.20, 187.56, 187.93, 188.29, 188.66, 189.02, 189.38, 189.75, 190.11},  // 230,
    /*26*/ {190.47, 190.84, 191.20, 191.56, 191.92, 192.29, 192.65, 193.01, 193.37, 193.74},  // 240,
    /*27*/ {194.10, 194.46, 194.82, 195.18, 195.55, 195.91, 196.27, 196.63, 196.99, 197.35},  // 250,
    /*28*/ {197.71, 198.07, 198.43, 198.79, 199.15, 199.51, 199.87, 200.23, 200.59, 200.95},  // 260,
    /*29*/ {201.31, 201.67, 202.03, 202.39, 202.75, 203.11, 203.47, 203.83, 204.19, 204.55},  // 270,
    /*30*/ {204.90, 205.26, 205.62, 205.98, 206.34, 206.70, 207.05, 207.41, 207.77, 208.13},  // 280,
    /*31*/ {208.48, 208.84, 209.20, 209.56, 209.91, 210.27, 210.63, 210.98, 211.34, 211.70},  // 290,
    /*32*/ {212.05, 212.41, 212.76, 213.12, 213.48, 213.83, 214.19, 214.54, 214.90, 215.25},  // 300,
    /*33*/ {215.61, 215.96, 216.32, 216.67, 217.03, 217.38, 217.74, 218.09, 218.44, 218.80},  // 310,
    /*34*/ {219.15, 219.51, 219.86, 220.21, 220.57, 220.92, 221.27, 221.63, 221.98, 222.33},  // 320,
    /*35*/ {222.68, 223.04, 223.39, 223.74, 224.09, 224.45, 224.80, 225.15, 225.50, 225.85},  // 330,
    /*36*/ {226.21, 226.56, 226.91, 227.26, 227.61, 227.96, 228.31, 228.66, 229.02, 229.37},  // 340,
    /*37*/ {229.72, 230.07, 230.42, 230.77, 231.12, 231.47, 231.82, 232.17, 232.52, 232.87},  // 350,
    /*38*/ {233.21, 233.56, 233.91, 234.26, 234.61, 234.96, 235.31, 235.66, 236.00, 236.35},  // 360,
    /*39*/ {236.70, 237.05, 237.40, 237.74, 238.09, 238.44, 238.79, 239.13, 239.48, 239.83},  // 370,
    /*40*/ {240.18, 240.52, 240.87, 241.22, 241.56, 241.91, 242.26, 242.60, 242.95, 243.29},  // 380,
    /*41*/ {243.64, 243.99, 244.33, 244.68, 245.02, 245.37, 245.71, 246.06, 246.40, 246.75}}; // 390,

// 直线方程 y=kx+b（x1>x2,y1>y2）
float Two_Point_Equation(float x1, float y1, float x2, float y2, float x)
{
    float slope_K, intercept_B = 0; // 斜率和截距

    // 计算斜率 K
    slope_K = (y2 - y1) / (x2 - x1);
    // 计算截距 B
    intercept_B = (y1 * x2 - y2 * x1) / (x2 - x1);

    return (slope_K * x + intercept_B);
}

// 获取参考点电压  mV
uint16_t bsp_Get_Reference_mV(void)
{
    return (REFERENCE_MV / 4096.0 * ADC_OBEY.a_Obj_Struct[0].a_Obj_Value);
}
// 获取PT100电阻  Ω
float bsp_Get_PT100_R(void)
{
    return ((float)PT100.Measure_mV * MEASURE_R / (REFERENCE_MV - PT100.Measure_mV));
}
// 获取温度数据
float bsp_Get_Temp_Data(void)
{
    uint8_t circulate_i = 0;

    static uint8_t Now_Rows = ROWS / 2; // 现在温度所在的行号
    static uint8_t Now_Cols = COLS / 2; // 现在温度所在的列号

    float coordinate_X1, coordinate_Y1, coordinate_X2, coordinate_Y2, coordinate_X, coordinate_Y = 0; // 坐标(X1,Y1)、(X2,Y2)、(当前阻值,当前温度)
    // #define ROWS 43 // 行数
    // #define COLS 10 // 列数

    PT100.Measure_mV = bsp_Get_Reference_mV(); // 读取参考点电压
    PT100.Measure_R = bsp_Get_PT100_R();       // 读取PT100阻值

    // 查找温度区间
    while (1) // 防止行和列超限
    {
        if (Now_Rows > ROWS && Now_Cols > COLS)
            return (399);

        if (PT100.Measure_R >= Table_Of_PT100[Now_Rows][Now_Cols] && PT100.Measure_R <= Table_Of_PT100[Now_Rows][Now_Cols + 1])
        {
            // printf("循环次数：%d找到位置在:", circulate_i);
            break;
        }
        else
        {
            if (PT100.Measure_R < Table_Of_PT100[Now_Rows][Now_Cols]) // 温度降低
            {
                if (Now_Cols == 0)
                {
                    Now_Rows--;   // 行号-1
                    Now_Cols = 9; // 列最大
                }
                else
                {
                    Now_Cols--; // 列号-1
                }
            }
            else if (PT100.Measure_R > Table_Of_PT100[Now_Rows][Now_Cols + 1]) // 温度升高
            {
                if (Now_Cols > COLS) // 如果列已满
                {
                    Now_Rows++;   // 行号+1
                    Now_Cols = 0; // 列清零
                }
                else
                {
                    Now_Cols++; // 列号+1
                }
            }
        }
        circulate_i++;
        if (Now_Rows == 0 && Now_Cols == 0)
            return (0);
    }

    if (Now_Cols == 0) // 如果列号为0
    {
        coordinate_X1 = Table_Of_PT100[Now_Rows - 1][9]; // X1=前一行的最后一个
        coordinate_Y1 = Temp_Of_PT100[Now_Rows - 1] + 9; // Y1=前一行的最后一个
    }
    else // 如果列号不为0
    {
        coordinate_X1 = Table_Of_PT100[Now_Rows][Now_Cols - 1]; // X1=本行本列的前一个
        coordinate_Y1 = Temp_Of_PT100[Now_Rows] + Now_Cols - 1; // Y1=本行本列的前一个
    }
    coordinate_X2 = Table_Of_PT100[Now_Rows][Now_Cols]; // X2=本行的第0个
    coordinate_Y2 = Temp_Of_PT100[Now_Rows] + Now_Cols; // Y2=本行的第0个
    coordinate_X = PT100.Measure_R;

    // printf("%d行,%d列,温度区间%.2f~%.2f℃  ", Now_Rows, Now_Cols, coordinate_Y1, coordinate_Y2);

    /*代入直线方程进行直线插补，求目标温度*/
    coordinate_Y = Two_Point_Equation(coordinate_X1, coordinate_Y1,
                                      coordinate_X2, coordinate_Y2,
                                      coordinate_X);

    // printf("当前温度%.2f℃ \r\n", coordinate_Y);

    return (coordinate_Y);
}
// PT100定时读取温度
void Ti_PT100_event(void)
{
    info.i_NowTemp = bsp_Get_Temp_Data();
    printf("当前温度%.2f℃ \r\n", info.i_NowTemp);
}
// PT100调试函数
void bsp_PT100_Debug(void)
{
    bsp_TIM_t Ti_BSP_ADC;
    bsp_TIM_t Ti_BSP_ADC_DATA;
    bsp_TIM_t Ti_PT100;

    bsp_Adc_Init(&ADC_OBEY);
    Ti_obj_New(&Ti_BSP_ADC, 10, Ti_RUN, Ti_BSP_ADC_event);
    Ti_obj_New(&Ti_BSP_ADC_DATA, 10 * ADC_OBJ_DATA_NUM, Ti_RUN, Ti_BSP_ADC_DATA_event);
    Ti_obj_New(&Ti_PT100, 500, Ti_RUN, Ti_PT100_event);

    // PA6引脚控制加热
    LL_GPIO_SetPinMode(GPIOA, LL_GPIO_PIN_6, LL_GPIO_MODE_OUTPUT); // 调试专用

    while (1)
    {

        bsp_Ti_All_Handle();
    }
}
